<?php

namespace app\common\model;

use think\Model;
use think\Db;
use app\common\traits\model\TraitModel;
/**
 * 提现
 */
class Cashing extends Model
{
	protected $autoWriteTimestamp = 'timestamp';
    protected $createTime         = 'created_at';
    protected $updateTime         = 'updated_at';
    protected $table              = 'withdraw_deposits';

	use TraitModel;

	const APPLY_STATUS    = 0;//申请中
	const PASSED_STATUS   = 1;//申请通过提现中
	const REFUSED_STATUS  = -1;//申请拒绝
	const COMPLETE_STATUS = 2;//提现完成
	
	/**
	 * 获取用户信息
	 */
	public function users($user_id){
		return model('user/Users')->where('id',$user_id)->value('real_name');
	}
	
	/**
	 * 获取提现信息列表
	 * @param $querys
	 */
	public function getCashingList($querys=[]){
		return $this->getInfoList($querys);		
	} 
	 
	 
	/**
	 * 获取提现信息
	 * @param  [type] $id    [description]
	 * @param  string $field [description]
	 * @return [type]        [description]
	 */
	public function getCashing($id, $field = '')
	{
		$cashing = $this->get($id);
		if ($field) {
            if (isset($cashing->$field)) {
                return $cashing->$field; 
            }
            return false;
        }
        return $cashing;
	}
	/**
	 * 提现申请
	 * @param [type] $user_id [description]
	 * @param [type] $amount [description]
	 */
    public function apply($user_id, $amount)
    {
    	$this->startTrans();
    	try {
    		if(!is_int($amount['amount']) || $amount['amount']<=0){
    			return false;
    		}
    		$this->reduceCapital($user_id, $amount['amount']);
    		$this->create(['user_id' => $user_id, 'amount' => $amount['amount'], 'bank_num' => $amount['bank_num'], 'open_bank' => $amount['open_bank'], 'currency' => 'CNY', 'status' => self::APPLY_STATUS], true);
    		$this->commit();
    	} catch (\Exception $e) {
    		$this->rollback();
    		return false;
    	}
        return true;
    }
    /**
     * 通过
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function passed($id)
    {
    	return $this->setStatus(['id' => $id],self::PASSED_STATUS);
    }
    /**
     * 拒绝
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function refused($id)
    {
    	$cashing = $this->getCashing($id);
    	if (!$cashing) {
    		return false;
    	}
    	if ($cashing->status == self::REFUSED_STATUS) {
    		return true;
    	}
    	$this->startTrans();
    	try {
    		$this->incCapital($cashing->user_id, $cashing->amount);
    		$this->setStatus(['id' => $id], self::REFUSED_STATUS);
    		$this->commit();
    	} catch (\Exception $e) {
    		$this->rollback();
    		return false;
    	}
        return true;
    }
    /**
     * 完成提现
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function complete($id)
    {
        return $this->setStatus(['id' => $id], self::COMPLETE_STATUS);;
    }
    private function getStatus($id)
    {
    	return $this->getCashing($id, 'status');
    }
    /**
     * 设置提现流水状态
     * @param [type] $where  [description]
     * @param [type] $status [description]
     */
    private function setStatus($where, $status = self::APPLY_STATUS)
    {
    	return $this->update(['status' => $status], $where, true);
    }
    /**
     * 提现同步扣除资金
     * @param  [type] $user_id [description]
     * @param  [type] $amount  [description]
     * @return [type]          [description]
     */
    private function reduceCapital($user_id, $amount)
    {
    	model('Capitals')->reduceCapital($amount, $user_id, ['type' => '提现', 'remark' => '提现'], true);
    }
    /**
     * 提现同步增加资金
     * @param  [type] $user_id [description]
     * @param  [type] $amount  [description]
     * @return [type]          [description]
     */
    private function incCapital($user_id, $amount)
    {
    	model('Capitals')->incCapital($amount, $user_id, ['type' => '提现', 'remark' => '提现失败']);
    }
}
